<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no, target-densitydpi=medium-dpi" />
<html>
  
  <head>
	 <meta http-equiv="content-type" content="text/html; charset=utf-8"/>    
	<title>Welcome to Yes or No</title>    
	
	  <style type = "text/css">
		  body{
			  font-family: Futura;
			  background-image: gray;
		  }
		  p{
			  color: maroon;
		  }
		  h1{
			  font-family: Zapfino;
			  color: gray;
			  font-size : 40;
			
		  }
	  </style>
	<script src="../googleAnalytics.js" type="text/javascript"></script>   
 <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=true&amp;key=ABQIAAAAHHQwI7Yl5wVURxsuvbc5jxQlGZdF2Aq1sJRccIBbnAqP2Aw7NRRxPN3evffiQF0l4H8NeKuUuMFNlw" type="text/javascript"></script>	<script type"text/javascript"> 
		window.onload=initialize;
		window.onunload=GUnload;
		
		var map;
		var geocoder;
		var myStatus;
 
		// Very Basic Map setup on canvas
		
	function view(arg){
     	if(arg=="1"){
          document.all.sub1.style.display="none";
          document.all.sub2.style.display="";
          document.all.sub3.style.display="";
     	}
     	else{
     
          	document.all.sub1.style.display="";
          	document.all.sub2.style.display="none";
          	document.all.sub3.style.display="none";
  		   }
 		}
		function initialize() {      
			if (GBrowserIsCompatible()) {        
				map = new GMap2(document.getElementById("map_canvas"));     
				geocoder = new GClientGeocoder();
				map.setCenter(new GLatLng(37.4419, -122.1419), 13);  
				map.disableDoubleClickZoom();
				map.addControl(new GSmallMapControl());
 
				addEvtListners();
			}    
		}
 
		function addEvtListners() {
 
			myEventListener1 = GEvent.addListener(map, "click", function(overlay, latlng) {    
				if (latlng) {  
					map.clearOverlays();
					geocoder.getLocations(latlng, showAddress);
					myStatus =	"The GPoint: "										+ map.fromLatLngToDivPixel(latlng) + "<br/>" + 
								"The GLatlng: "										+ latlng.toString() + "<br/>" + 
								"Zoom level(0~19): "								+ map.getZoom() + "<br/>" + 
								"<br/>";  
				
				}
			});
 
		}
 
		function showAddress(response) {  
			if (!response || response.Status.code != 200) {    
				alert("Status Code:" + response.Status.code);  
			} 
			else {    
				place = response.Placemark[0];    
				point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
				marker = new GMarker(point);
				map.addOverlay(marker);
 
				document.q.address.value=response.Placemark[0].address; 
				myStatus =response.Placemark[0].address;      
				
			}
		}
 
		function refreshStatus() {
			document.getElementById("status").innerHTML = "";	
			counter1=0;
		}
 
	</script> 
  </head>
  <body>

    <h1>Send</h1>
    <?php
    $output_form = false;	//html 폼을 출력할지 말아야할지를 결정하는 변수
		$message = NULL;	//message 초기화 
		$reciever =NULL; // reciever 초기화
    $subject = NULL; // subject 초기화
		
  
    
    if( isset( $_POST['submit'] ) ){
      $message = $_POST['message'];
      $reciever = $_POST['reciever'];
      $subject = $_POST['subject'];
    
      if( empty( $subject ) ){
     		echo 'You forgot the subject.<br />';
				$output_form = true;      
      }
      if( empty( $message ) ){
     		echo 'You forgot the message.<br />';
				$output_form = true;
      }
    }else{
      $output_form = true;
    }
    
          
   
   if( $output_form ){
    ?>
		
		 <table>
          <tr>
            <td>
              <p>To:</p>
            </td>
            <td>
              <input type="text" id ="reciever" name="reciever" value="" cols="30"/>
            </td>
            
          </tr>
          <tr>
            <td>
              <p>Subject:</p>
            </td>
            <td>
              <input type="text" id ="subject" name="subject" value= "<?php  echo $subject;  ?>" />
            </td>
          </tr>
          <tr>
            <td>
              <p>Message:</p>
            </td>
            <td>
              <textarea name="message" id="message" rows="10" cols="30"  ><?php echo $message;  ?></textarea>
            </td>
          </tr>
        </table>
        <form name="q"> 
 			<table>
 				<tr id ="sub1">
 					<td onclick="javascript:view('1')">
 						<p>Open Map</p>
 					</td>
  				</tr>
  				<tr id ="sub2" style="display:none">
  					<td onclick="javascript:view('2')">
  						<p>Close Map</p>
  					</td>
  				</tr>
  				<tr id ="sub3" style="display:none">
  					<td>
						<div id="map_canvas" style="margin: 1em 1em; width:300px; height: 300px;"></div> 
					</td>
				</tr>	
			<table>
				<tr>
					<td>
						<p>Address:</p>
					</td>
					<td>
						<textarea name="address" id="address" rows="3" cols="30"  >
					</td>
				</tr>	
			</table>
      <table>
        <form action = ""
          <?php  echo $_SERVER['PHP_SELF'];  ?> " method="post" >
          <input type="submit" value="submit" name="submit" />
        </form>
      </table>
		</form>
   
    <?php
    }else{
 			$dbc = mysqli_connect( 'localhost' ,'root' , '1234', 'teresa' ) or die( 'Error connecting to MySQL server.');
			
      $sender = $_COOKIE[ 'user_email' ];
  
      // insert message data
			$query = "INSERT INTO teresa_message( sender, reciever, subject, message  )".
	  		"VALUES( '$sender' , 'sender' , '$subject' ,'$message'  )"; 
      mysqli_query( $dbc, $query ) or die('ERROR INSERT MESSAGE queerying database.');
      
      // select lately data
      $query = "SELECT * FROM teresa_message WHERE sender = '$sender' ORDER BY message_id DESC";
	  	$data = mysqli_query( $dbc , $query) or die('ERROR SELECT queerying database.');
      
      $row = mysqli_fetch_array( $data );
      
      $maximum_count = $row['maximum'];
      $message_id = $row['message_id'];
      $user_id = $_COOKIE[ 'user_id' ];
      
      // linked meessage and sender
      $query = "INSERT INTO teresa_response( maximum, user_id, message_id )".
	    	"VALUES( '$maximum_count' , '$user_id' , '$message_id' )"; 
      mysqli_query( $dbc, $query ) or die('ERROR INSERT RESPONSE queerying database.');     
      
      // find response id
      $query = "SELECT * FROM teresa_response WHERE message_id = '$message_id' ORDER BY message_id DESC";
	  	$data = mysqli_query( $dbc , $query) or die('ERROR SELECT queerying database.');

      $row = mysqli_fetch_array( $data );

      $response_id = $row['response_id'];       
      $maximum = $row['maximum'];
      $response = $row['response'];
      
      //close database
      mysqli_close($dbc);	
      $home_url = 'http://' . $_SERVER['HTTP_HOST'] . '/sender_reciever.php?message_id=' . $message_id . '&user_id=' . $user_id . '&response_id=' . $response_id . '&maximum=' . $maximum . '&response=' . $response;
		  header('Location: ' . $home_url );         
    }
    ?>

  </body>
	
</html>	